<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">
		<el-form-item label="排序" prop="sortNo">
			<el-input-number v-model="form.sortNo" :min="1" style="width: 100%;"></el-input-number>
		</el-form-item>
		<el-form-item label="虚拟组织" prop="tempOrgId">
			<el-select v-model="form.tempOrgId" placeholder="请选择" @change="getTempDeptList" style="width: 100%">
				<el-option v-for="item in tempOrgList" :key="item.tempOrgId" :label="item.orgName" :value="item.tempOrgId">
				</el-option>
			</el-select>
		</el-form-item>
		<el-form-item label="所在部门" prop="deptId" v-show="form.tempOrgId!=''">
			<el-cascader v-model="form.deptId" :options="depts" :props="deptsProps" :show-all-levels="false" clearable style="width: 100%;"></el-cascader>
		</el-form-item>
			<el-form-item label="用户账号" prop="accountId">
				<select-user-dialog v-model:accountId="form.accountId" :mode="false"/>
			</el-form-item>
			<el-form-item label="个人领导" prop="leadId">
				<select-user-dialog v-model:accountId="this.form.leadId" :mode="false"/>
			</el-form-item>
			<el-form-item label="行政级别" prop="levelId" v-show="form.tempOrgId!=''">
				<el-select v-model="form.levelId" placeholder="请选择" style="width: 100%">
					<el-option v-for="item in userLevel" :key="item.value" :label="item.label" :value="item.value"></el-option>
				</el-select>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import msgBoxCheckBox from "@/views/module/msgBoxCheckBox";
	export default {
		emits: ['success', 'closed'],
		components: {
			selectUserDialog
		},
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增用户',
					edit: '编辑用户',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					sortNo:0,
					accountId:"",
					tempOrgId: "",
					deptId: "",
					leadId:"",
					levelId:""
				},
				tempOrgList:[],
				//验证规则
				rules: {
					sortNo: [
						{required: true, message: '请输入排序', trigger: 'change'}
					],
					accountId: [
						{required: true, message: '请选择用户账号'}
					],
					tempOrgId: [
						{required: true, message: '请选择虚拟组织'}
					],
					deptId: [
						{required: true, message: '请选择虚拟部门'}
					],
					levelId: [
						{required: true, message: '请选择虚拟行政职务'}
					],
				},
				depts: [],
				deptsProps: {
					label:"deptName",
					value: "deptId",
					emitPath: false,
					checkStrictly: true
				},
				userLevel:[]
			}
		},
		mounted() {
			this.getTempUnitList();
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			async getTempUnitList(){
				var res = await this.$API.system.tempOrg.getTempUnitList.get();
				this.tempOrgList = res.data;
			},
			async getUserLevel(val){
				var res = await this.$API.system.tempOrg.getTempUserLevelForSelect.get({tempOrgId:val});
				this.userLevel = res.data;
			},
			async getTempDeptList(val){
				this.getUserLevel(val);
				var res = await this.$API.system.tempOrg.getTempDeptList.get({tempOrgId:val});
				this.depts = res.data;
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res;
						if(this.mode=="add")
						{
							res = await this.$API.system.tempOrg.insertTempUser.post(this.form);
						}else if(this.mode=="edit")
						{
							res = await this.$API.system.tempOrg.updateTempUser.post(this.form);
						}
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			setData(data){
				this.getTempDeptList(data.tempOrgId);
				this.getUserLevel(data.tempOrgId)
				this.form.sortNo = data.sortNo;
				this.form.userId = data.userId;
				this.form.accountId = data.accountId;
				this.form.tempOrgId = data.tempOrgId;
				this.form.leadId = data.leadId;
				this.form.deptId = data.deptId;
				this.form.levelId=data.levelId;
			},
		}
	}
</script>

<style>
</style>
